using System;
using System.Collections.Generic;
using System.Text;
using Drawing = System.Drawing;
using Imaging = System.Drawing.Imaging;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using System.Drawing;

namespace Scurvy.Media.Pipeline.Video
{
    internal abstract class PlatformSurface
    {
        protected PlatformSurface()
        {
        }

        public static PlatformSurface Create()
        {
            if (Scurvy.Media.Pipeline.Platform.IsXBox)
            {
                return new XboxSurface();
            }
            else if (Scurvy.Media.Pipeline.Platform.IsZune)
            {
                return new ZuneSurface();
            }

            return new x86Surface();
        }

        public abstract BitmapContent FromGdiBitmap(Drawing.Bitmap bitmap);

        public abstract void SetData(BitmapContent b, byte[] pixelBuffer);

        public abstract Imaging.PixelFormat GdiPixelFormat { get; }

        public abstract int SurfaceMultiplier { get; }


        #region Contained Classes
        private class x86Surface : PlatformSurface
        {
            public x86Surface()
            {
                Console.WriteLine("COMPILING FOR x86");
            }

            public override BitmapContent FromGdiBitmap(System.Drawing.Bitmap bitmap)
            {
                return new PixelBitmapContent<Microsoft.Xna.Framework.Graphics.PackedVector.Bgr565>(bitmap.Width, bitmap.Height);
            }

            public override void SetData(BitmapContent b, byte[] pixelBuffer)
            {
                b.SetPixelData(pixelBuffer);
            }

            public override void WriteToXnb(Microsoft.Xna.Framework.Content.Pipeline.Serialization.Compiler.ContentWriter output, byte[] pix)
            {

                output.Write(pix.Length);
                output.Write(pix);
            }

            public override System.Drawing.Imaging.PixelFormat GdiPixelFormat
            {
                get { return Imaging.PixelFormat.Format16bppRgb565; }
            }

            public override int SurfaceMultiplier
            {
                get { return 2; }
            }
        }

        private class XboxSurface : PlatformSurface
        {
            public XboxSurface()
            {
                Console.WriteLine("COMPILING FOR XBOX");
            }

            public override System.Drawing.Imaging.PixelFormat GdiPixelFormat
            {
                get { return Imaging.PixelFormat.Format32bppArgb; }
            }

            public override void SetData(BitmapContent b, byte[] pix)
            {
                byte[] xpix = new byte[pix.Length];
                for (int i = 0; i < pix.Length; i += 4)
                {

                    int first = i, second = i + 1, third = i + 2, fourth = i + 3;
                    xpix[first] = pix[third]; //x=red
                    xpix[second] = pix[second]; //x=green 
                    xpix[third] = pix[first];//x=blue 
                    xpix[fourth] = pix[fourth];//x=alpha  
                }

                b.SetPixelData(xpix);
            }

            public override BitmapContent FromGdiBitmap(System.Drawing.Bitmap bitmap)
            {
                return new PixelBitmapContent<Microsoft.Xna.Framework.Graphics.PackedVector.Rgba32>(bitmap.Width, bitmap.Height);
            }

            public override void WriteToXnb(Microsoft.Xna.Framework.Content.Pipeline.Serialization.Compiler.ContentWriter output, byte[] pix)
            {

                byte[] xpix = new byte[pix.Length];
                for (int i = 0; i < pix.Length; i += 2)
                {

                    int first = i, second = i + 1;
                    xpix[first] = pix[second];
                    xpix[second] = pix[first];
                }

                output.Write(xpix.Length);
                output.Write(xpix);
            }

            public override int SurfaceMultiplier
            {
                get { return 4; }
            }
        }

        private class ZuneSurface : PlatformSurface
        {
            public ZuneSurface()
            {
                Console.WriteLine("Compiling for Zune");
            }

            public override System.Drawing.Imaging.PixelFormat GdiPixelFormat
            {
                get { return Imaging.PixelFormat.Format32bppArgb; }
            }

            public override BitmapContent FromGdiBitmap(Bitmap bitmap)
            {
                return new PixelBitmapContent<Microsoft.Xna.Framework.Graphics.PackedVector.Rgba32>(bitmap.Width, bitmap.Height);
            }

            public override void SetData(BitmapContent b, byte[] pixelBuffer)
            {
                b.SetPixelData(pixelBuffer);
            }

            public override void WriteToXnb(Microsoft.Xna.Framework.Content.Pipeline.Serialization.Compiler.ContentWriter output, byte[] pix)
            {

                output.Write(pix.Length);
                output.Write(pix);
            }

            public override int SurfaceMultiplier
            {
                get { return 4; }
            }
        }

        #endregion

        public abstract void WriteToXnb(Microsoft.Xna.Framework.Content.Pipeline.Serialization.Compiler.ContentWriter output, byte[] pix);
    }
}
